#include "chooselevelscene.h"

#include <qmainwindow.h>
#include <QMenu>
#include <QMenuBar>
#include <qaction.h>
#include <qpainter.h>
#include <qpixmap.h>
#include <mypushbutton.h>
#include <QLabel>
#include <QDebug>
#include <QTimer.h>
#include <QSoundEffect>

ChooseLevelScene::ChooseLevelScene(QWidget *parent)
    : QMainWindow{parent}
{
    setFixedSize(320,588);

    setWindowIcon(QIcon(":/res/Coin0001.png"));

    setWindowTitle("选择关卡");

    // 创建菜单栏
    QMenuBar *mbar = menuBar();
    setMenuBar(mbar);

    //移除状态栏
    setStatusBar(nullptr);

    //创建开始菜单
    QMenu *startMenu = mbar->addMenu("开始");
    QAction *quitAction = startMenu->addAction("退出");
    connect(quitAction,&QAction::triggered,this,[=](){
        this->close();
    });

    // 返回按钮音效
    QSoundEffect *backSound = new QSoundEffect(this);
    backSound->setSource(QUrl::fromLocalFile(":/res/TapButtonSound.wav"));

    // 返回按钮
    MyPushButton *backBtn = new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width() - backBtn->width(),this->height() - backBtn->height() - 15);

    connect(backBtn,&MyPushButton::clicked,this,[=](){
        backSound->play();
        // 发送返回信号
        emit this->chooseBack();
    });

    // 选择关卡音效
    QSoundEffect *sound = new QSoundEffect(this);
    sound->setSource(QUrl::fromLocalFile(":/res/TapButtonSound.wav"));

    //选择关卡的按钮
    for (int i = 0; i < 20; i++) {
        MyPushButton *menuBtn = new MyPushButton(":/res/LevelIcon.png");
        menuBtn->setParent(this);
        menuBtn->move(25+i%4*70,130+i/4*70);

        connect(menuBtn,&MyPushButton::clicked,this,[=](){
            sound->play();
            QString str = QString("您选择的是第 %1 关").arg(i+1);
            qDebug() << str;
            if(pScene == NULL){
                this->hide();
                pScene = new PlayScene(i+1);
                pScene->setGeometry(this->geometry());
                pScene->show();
                connect(pScene,&PlayScene::chooseBack,this,[=](){
                    QTimer::singleShot(200,this,[=](){
                        this->setGeometry(pScene->geometry());
                        this->show();
                        delete pScene;
                        pScene = NULL;

                    });
                });
            }
        });

        QLabel *label = new QLabel();
        label->setParent(this);
        label->setFixedSize(menuBtn->width(),menuBtn->height());
        label->setText(QString::number(i+1));
        label->move(25+i%4*70,130+i/4*70);
        label->setAlignment(Qt::AlignCenter|Qt::AlignVCenter);
        // 让鼠标事件穿透
        label->setAttribute(Qt::WA_TransparentForMouseEvents);
    }
}

void ChooseLevelScene::paintEvent(QPaintEvent *){
    QPainter painter(this);

    QPixmap pix;
    pix.load(":/res/OtherSceneBg.png");
    painter.drawPixmap(0,0,pix.width(),pix.height(),pix);

    pix.load(":/res/Title.png");
    pix.scaled(pix.width()*0.5,pix.height()*0.5);
    painter.drawPixmap(20,30,pix);
}
